var express = require('express');
var path = require("path");
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var fs = require('fs');

//var session = require('express-session');
var hbs=require('hbs');


/*
var routes = require('./routes/index');
var login = require('./routes/login');
var updateCache = require('./routes/updateCache');
var memberAdmin = require('./routes/memberAdmin');
var shopList= require('./routes/shopList');
var navadmin= require('./routes/navadmin');
var searchkey= require('./routes/searchkey');
var kuaiXuan= require('./routes/kuaiXuan');
var indexAdvPic= require('./routes/indexAdvPic');
var indexTuangou= require('./routes/indexTuangou');
var indexMaterial= require('./routes/indexMaterial');
var indexHots= require('./routes/indexHots');
var indexTuku= require('./routes/indexTuku');
var indexGoods= require('./routes/indexGoods');
var indexBrand= require('./routes/indexBrand');
var indexLinks= require('./routes/indexLinks');
var materialHot= require('./routes/materialHot');
var materialPrc= require('./routes/materialPrc');
var materialGuide= require('./routes/materialGuide');
var jiajuHot= require('./routes/jiajuHot');
var jiajuPrc= require('./routes/jiajuPrc');
var materialGuide= require('./routes/materialGuide');
var materialGuide= require('./routes/materialGuide');
var materialGuide= require('./routes/materialGuide');
var materialGuide= require('./routes/materialGuide');
var materialGuide= require('./routes/materialGuide');
*/
//var users = require('./routes/users');


var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.engine('html',hbs.__express);
app.set('view engine','html');

app.set('routes',__dirname + '/routes/');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


var routes=app.get("routes");
console.log(routes);
fs.readdirSync(routes).forEach(function(fileName) {
  var filePath = routes + fileName;
  var rname=fileName.substr(0, fileName.lastIndexOf("."));
  if(!fs.lstatSync(filePath).isDirectory()) {
    if(rname === "index"){
      app.use("/",require(filePath));
      app.use("/index.html",require(filePath));
    }else {
      app.use("/" + rname + ".html",require(filePath));
    }
  }
});
/*app.use('/', routes);
app.use('/index.html', routes);
app.use('/login.html', login);
app.use('/updateCache.html',updateCache);
app.use('/memberAdmin.html',memberAdmin);
app.use('/shopList.html',shopList);
app.use('/navadmin.html',navadmin);
app.use('/searchkey.html',searchkey);
app.use('/kuaiXuan.html',kuaiXuan);
app.use('/indexAdvPic.html',indexAdvPic);
app.use('/indexTuangou.html',indexTuangou);
app.use('/indexMaterial.html',indexMaterial);
app.use('/indexHots.html',indexHots);
app.use('/indexTuku.html',indexTuku);
app.use('/indexGoods.html',indexGoods);
app.use('/indexBrand.html',indexBrand);
app.use('/indexLinks.html',indexLinks);
app.use('/materialHot.html',materialHot);
app.use('/materialPrc.html',materialPrc);
app.use('/materialGuide.html',materialGuide);
app.use('/jiajuHot.html',jiajuHot);
app.use('/jiajuPrc.html',jiajuPrc);
app.use('/materialPrc.html',materialPrc);
app.use('/materialPrc.html',materialPrc);
app.use('/materialPrc.html',materialPrc);*/

//app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});




/*app.post(/^\/(op)\/(.*)/, sendOp);
function sendOp(req, res, next) {
  req.params = __.extend(req.params, req.query);
  req.params = __.extend(req.params, req.body);
  if (req.url === '/op/login') {
    users.login(req, res, function (result) {
      var stringfyResult = JSON.stringify(result);
      res.end(stringfyResult);
    });

  }*/



module.exports = app;